package com.truongnha.clienttn.presentation.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import com.truongnha.clienttn.R;
import com.truongnha.clienttn.business.model.TeacherAttendModel;
import com.truongnha.clienttn.business.service.TeacherAttendService;
import com.truongnha.clienttn.presentation.extension.TriToggleButton;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class TeacherAttendAdapter extends ArrayAdapter<TeacherAttendModel> {
	final Toast toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT);
	private final List<TeacherAttendModel> list;
	private List<TeacherAttendModel> original;
	private Dictionary<Integer, Integer> diff = new Hashtable<Integer, Integer>();
	private Boolean copy = true;// Only copy at the first time, change to false
								// after that.
	private final Activity context;
	private Calendar myCalendar;
	private Boolean submitPermission;

	public TeacherAttendAdapter(Activity context,
			List<TeacherAttendModel> list, Boolean submitPermission, Calendar myCalendar) {
		super(context, R.layout.teacher_attend_item, list);
		this.context = context;
		this.list = list;
		this.submitPermission = submitPermission;
		this.myCalendar = myCalendar;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		diff = new Hashtable<Integer, Integer>();
		copy = true;
	};

	public void savedClick() {
		diff = new Hashtable<Integer, Integer>();
		copy = true;
	}

	public List<TeacherAttendModel> copyFrom(List<TeacherAttendModel> B) {
		int size = B.size();
		List<TeacherAttendModel> A = new ArrayList<TeacherAttendModel>(size);
		for (int i = 0; i < size; i++) {
			TeacherAttendModel aModel = B.get(i);
			A.add(new TeacherAttendModel(aModel.getId(), aModel.getIndex(),
					aModel.getName(), aModel.getBirth(), aModel.getSex(),
					aModel.getStatus()));
		}
		return A;
	}

	public void submitAttendace(TeacherAttendModel teacherAttendModel) {
		toast.setText(teacherAttendModel.getName()
				+ "\nNghỉ học không phép " + teacherAttendModel.getStatus());
		toast.show();
		TeacherAttendService  teacherAttendService = new TeacherAttendService(getContext());
		
		int year = myCalendar.get(Calendar.YEAR);
		int month = myCalendar.get(Calendar.MONTH) + 1;
		int day = myCalendar.get(Calendar.DAY_OF_MONTH);
		String date = day + "-" + month + "-" + year;
		TeacherAttendService.submitAttendance(teacherAttendModel,date);
	}
	class ViewHolder {
		protected TextView name;
		protected TextView status;
		protected TriToggleButton triToggle;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (copy) {
			this.original = copyFrom(list);
			copy = false;
		}
		View view = null;
		if (convertView == null) {// Convert view equal null at the first time
									// only
			Log.d("log", "teacher attend adapter 1");
			LayoutInflater inflator = context.getLayoutInflater();

			Log.d("log", "teacher attend adapter 2");
			view = inflator.inflate(R.layout.teacher_attend_item, null);

			Log.d("log", "teacher attend adapter 3");
			final ViewHolder viewHolder = new ViewHolder();

			Log.d("log", "teacher attend adapter 4");
			viewHolder.status = (TextView) view.findViewById(R.id.std_status);

			Log.d("log", "teacher attend adapter 5");
			viewHolder.status.setText(list.get(position).getSex() + "-"
					+ list.get(position).getBirth());

			Log.d("log", "teacher attend adapter 6");
			viewHolder.triToggle = (TriToggleButton) view
					.findViewById(R.id.triToggleButton);

			Log.d("log", "teacher attend adapter 7");
			view.setTag(viewHolder);

			Log.d("log", "teacher attend adapter 8");
			viewHolder.triToggle.setTag(list.get(position));

			Log.d("log", "teacher attend adapter 9");
			if (submitPermission) {// Only mainteacher have permission to change
									// the attendance
				viewHolder.triToggle
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								// TODO Auto-generated method stub
								final TeacherAttendModel element = (TeacherAttendModel) viewHolder.triToggle
										.getTag();
								int triState = viewHolder.triToggle.getState();
								switch (triState) {
								case 0:// Nghi hoc ko phep
								{
									element.setStatus(0);
									// result[0] = result[0] + 1;
									// btn_k.setText(result[0] + " - K");
									//compare(element.getIndex() - 1);
//									toast.setText(element.getName()
//											+ "\nNghỉ học không phép");
//									toast.show();
									break;
								}
								case 1:// Nghi hoc co phep
								{
									element.setStatus(1);
									// result[0] = result[0] - 1;
									// result[1] = result[1] + 1;
									// btn_k.setText(result[0] + " - K");
									// btn_c.setText(result[1] + " - P");
									//compare(element.getIndex() - 1);
//									toast.setText(element.getName()
//											+ "\nNghỉ học có phép");
//									toast.show();
									break;
								}
								case 2:// Di hoc muon
								{
									element.setStatus(2);
									// result[1] = result[1] - 1;
									// result[2] = result[2] + 1;
									// btn_c.setText(result[1] + " - P");
									// btn_m.setText(result[2] + " - M");
									//compare(element.getIndex() - 1);
//									toast.setText(element.getName()
//											+ "\n Đi học muộn");
//									toast.show();
									break;
								}
								case 3:// Di hoc
								{
									element.setStatus(3);
									// result[2] = result[2] - 1;
									// btn_m.setText(result[2] + " - M");
									//compare(element.getIndex() - 1);
//									toast.setText(element.getName()
//											+ "\n Đi học");
//									toast.show();
									break;
								}
								default:
									break;
								}
								submitAttendace(element);
							}
						});// End SetOnclickListenner
			} else {// Clickable = fal se for other teacher
				viewHolder.triToggle.setBackgroundDrawable(null);
				viewHolder.triToggle.setClickable(false);
			}
			viewHolder.name = (TextView) view.findViewById(R.id.std_name);
			viewHolder.name.setText(list.get(position).getIndex() + "- "
					+ list.get(position).getName());
		} else {
			Log.e("adapter", "use convertview ");
			view = convertView;
			((ViewHolder) view.getTag()).triToggle.setTag(list.get(position));
		}// end if.
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.name.setText(String.format("%2d. %s", list.get(position)
				.getIndex() + 1, list.get(position).getName()));
		holder.triToggle.setState(list.get(position).getStatus());
		return view;
	}

}
